<!--
 * @Author: your name
 * @Date: 2021-12-06 13:56:32
 * @LastEditTime: 2021-12-19 21:37:32
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \homeStayMS\src\main\resources\static\vue\Register\Register.vue
-->
<template>
  <section class="main-contain">
    <el-card class="login-frame">
      <span class="title-head">创 建 账 号</span>
      <!-- label-width="80px" -->
      <section>
        <el-form ref="form" :model="form" :rules="registerFormRules">
          <el-form-item prop="username">
            <el-input
              prefix-icon="el-icon-user"
              placeholder="用户名"
              v-model="form.username"
            >
            </el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input
              prefix-icon="el-icon-lock"
              placeholder="请输入密码"
              v-model="form.password"
              show-password
            >
            </el-input>
          </el-form-item>

          <el-form-item prop="repwd">
            <el-input
              prefix-icon="el-icon-lock"
              placeholder="请再次确认密码"
              v-model="form.repwd"
              show-password
            ></el-input>
          </el-form-item>
        </el-form>

        <p class="footer-text-frame">
          <el-button
            size="small"
            class="comfirm-btn"
            type="primary"
            @click="registerUser()"
          >
            立即注册
          </el-button>
          <span>已有账号,</span>
          <span class="register" @click="toRegisterPage()">立即登录</span>
        </p>
      </section>
    </el-card>
  </section>
</template>

<script>
import api from '../../js/api/api.js';

export default {
  name: 'Register',

  data() {
    return {
      form: {
        username: '',
        password: '',
        repwd: '',
      },

      //表单的验证规则对象
      registerFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 3,
            max: 15,
            message: '长度要 3 到 15 个字符',
            trigger: 'blur',
          },
        ],

        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 3,
            max: 15,
            message: '长度要 3 到 15 个字符',
            trigger: 'blur',
          },
        ],

        repwd: [
          {
            validator: this.isPasswordRight,
            required: true,
            trigger: 'blur',
          },
        ],
      },
    };
  },

  methods: {
    // 再次输入的密码是否正确？
    isPasswordRight(rule, value, callback) {
      if (!value) {
        callback(new Error(' 请再次输入密码 '));
      }
      if (this.form.password !== value) {
        callback(new Error(' 两次输入的密码不相同 '));
      }
    },

    /** 检验表单内容是否都通过了校验？ 本方法借助于elementUI的el-form组件的校验方法 */
    isFormAllPassValidate() {
      // 默认全部的内容都填写完毕
      let res = true;
      this.$refs['form'].validate((validateRes, validateItemObj) => {
        if (!validateRes) {
          res = false;
          return false;
        }
      });
      return res;
    },

    /** 跳转到登陆页。 */
    toRegisterPage() {
      window.location = '/login';
    },

    /** 注册账户 */
    registerUser() {
      if (this.isFormAllPassValidate()) {
        api.registerUser(this.form).then((res) => {
          if (res.status === 200) {
            const data = res.data;
            if (data.code === '200' && data.status) {
              this.$message.success(' 注册成功！ ');
              setTimeout(() => {
                window.location = data.url;
              }, 2500);
            } else {
              this.$message.error(' 注册失败！ ');
            }
          }
        });
      } else {
        this.$message.error(
          ' 你尚未填写完全部的内容，或填写不正确，请重新检查输入！ '
        );
      }
    },
  },
};
</script>

<style scoped>
.main-contain {
  width: 90vw;
  height: 75vh;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}

/* 注册框 */
.login-frame {
  margin-top: 40px;
  width: 18%;
  height: 40%;
}

/*注冊字样*/
.title-head {
  display: flex;
  font-size: 24px;
  font-weight: 800;
  color: #595959;
  line-height: 32px;
  justify-content: center;
  margin-top: -15px;
  margin-bottom: 10px;
}



/*注册按钮*/
.comfirm-btn {
  width: 50%;
  background-color: rgb(102, 177, 255);
  font-size: 20px;
  font-weight: 400;

  border-color: #fff;
}

/* 底部提示文本框 */
.footer-text-frame {
  margin-top: 0px;
  display: flex;
  justify-content: right;
}
/*注册按钮*/
.comfirm-btn {
  margin-right: 20px;
}

/*立即登录：字体样式*/
.footer-text-frame span {
  margin-top: 16px;
  font-size: 14px;
}

/* 注册字样 */
.register {
  color: rgb(107, 197, 164);
}

.register:hover {
  cursor: pointer;
}
</style>
